#include "mpi.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
const int MAX_STRING = 100;

int run_kernel();

int main(int argc, char** argv)
{
    char greeting[MAX_STRING];
    int comm_sz;
    int my_rank;

    MPI_Init(NULL,NULL);
    MPI_Comm_size(MPI_COMM_WORLD,&comm_sz);
    MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
 
    if(my_rank==1){
        sprintf(greeting,"Greeting from process %d of %d!",my_rank,comm_sz);
        run_kernel();
        MPI_Send(greeting,strlen(greeting)+1,MPI_CHAR,0,0,MPI_COMM_WORLD);
        printf("rank %d has returned from MPI_Send\n",my_rank);
    }else if(my_rank==2){
        sprintf(greeting,"Greeting from process %d of %d!",my_rank,comm_sz);
        run_kernel();
        MPI_Send(greeting,strlen(greeting)+1,MPI_CHAR,0,0,MPI_COMM_WORLD);
        printf("rank %d has returned from MPI_Send\n",my_rank);
    }else if(my_rank==3){
        sprintf(greeting,"Greeting from process %d of %d!",my_rank,comm_sz);
        MPI_Send(greeting,strlen(greeting)+1,MPI_CHAR,0,0,MPI_COMM_WORLD);
        run_kernel();
        printf("rank %d has returned from MPI_Send\n",my_rank);
    }else if(my_rank==0){
        MPI_Recv(greeting,MAX_STRING,MPI_CHAR,1,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        printf("%s\n",greeting);
        MPI_Recv(greeting,MAX_STRING,MPI_CHAR,2,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        printf("%s\n",greeting);
        MPI_Recv(greeting,MAX_STRING,MPI_CHAR,3,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        printf("%s\n",greeting);
    }
    MPI_Finalize();
    printf("###rank %d finished\n", my_rank);
    return 0;
}
